/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.ExecXML;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author HoangLiem
 */
public class AddForeignkeyModel extends AbstractTableModel {

    private ArrayList<String> columnNames;
    private ArrayList<ArrayList<Object>> data;
    private ArrayList<Object> longValues;

    public ArrayList<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(ArrayList<String> columnNames) {
        this.columnNames = columnNames;
    }

    public ArrayList<ArrayList<Object>> getData() {
        return data;
    }

    public void setData(ArrayList<ArrayList<Object>> data) {
        this.data = data;
    }

    public ArrayList<Object> getLongValues() {
        return longValues;
    }

    public void setLongValues(ArrayList<Object> longValues) {
        this.longValues = longValues;
    }

    public ArrayList<Object> initNewRow() {
        ArrayList<Object> row = new ArrayList<Object>();
        row.add(new String(""));
        row.add(new Boolean(false));
        row.add(new String(""));
        return row;
    }

    public ArrayList<Object> initLongValues() {
        ArrayList<Object> longVal = new ArrayList<Object>();
        Object[] objs = new Object[]{"", Boolean.TRUE, ""};
        for (int i = 0; i < objs.length; i++) {
            longVal.add(objs[i]);
        }
        return longVal;
    }

    public AddForeignkeyModel() {
        columnNames = new ArrayList<String>();
        columnNames.add("Field Name");
        columnNames.add("Foreign Key");
        columnNames.add("ID Reference");
        data = new ArrayList<ArrayList<Object>>();
        for (int i = 0; i < 5; i++) {
            data.add(initNewRow());
        }
        longValues = initLongValues();
    }

    public AddForeignkeyModel(ArrayList<ArrayList<Object>> data) {
        columnNames = new ArrayList<String>();
        columnNames.add("Field Name");
        columnNames.add("Foreign Key");
        columnNames.add("ID Reference");
        this.data = data;
        longValues = initLongValues();
    }

    public AddForeignkeyModel(ArrayList<String> columnNames, ArrayList<ArrayList<Object>> data, ArrayList<Object> longValues) {
        this.columnNames = columnNames;
        this.data = data;
        longValues = initLongValues();
        this.longValues = longValues;
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames.get(col);
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data.get(row).get(col);
    }

    /*
     * Định nghĩa cách hiển thị lên table
     */
    @Override
    public Class getColumnClass(int c) {
        if (getValueAt(0, c) == null) {
            return String.class;
        }
        return getValueAt(0, c).getClass();
    }

    /*
     * Cho phep chinh sua tai cell
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == 0) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * set gia tri tai một cell trong table
     */
    public void setValue(Object value, int row, int col) {
        ArrayList<Object> oldRow = data.get(row);
        oldRow.add(col, value);
        oldRow.remove(col + 1);
        data.add(row, oldRow);
        data.remove(row + 1);
    }

    /*
     * Cap nhat data tren cell
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
        setValue(value, row, col);
        fireTableCellUpdated(row, col);
    }
}
